from plugin.dk.Base import Base
from . import docker


class Container(Base):

    def __init__(self):
        pass

    def list(self, all=False):
        """
        获取已经创建的container
        默认获取正在运行的
        :param all:
        :return:
        """
        result = []
        for image in docker.containers.list(all):
            result.append(image.attrs)
        return result

    def stop(self, containerId):
        """
        停止一个container的运行
        :param containerId:
        :return:
        """
        container = docker.containers.get(containerId)
        return container.stop()

    def start(self, containerId):
        """
        开始运行一个container
        :param containerId:
        :return:
        """
        container = docker.containers.get(containerId)
        return container.start()

    def restart(self, containerId):
        """
        重新启动一个container
        :param containerid:
        :return:
        """
        container = docker.containers.get(containerId)
        return container.restart()

    def remove(self, containerId):
        """
        删除一个container
        :param containerId:
        :return:
        """
        container = docker.containers.get(containerId)
        return container.remove()

    def create(self, image, command=None, **kwargs):
        """
        创建一个container
        :param image 镜像
        :param command 镜像
        :param args 可选配置参数
        :return:
        """
        return docker.containers.create(image, command, **kwargs)

    def cmd(self, containerId,cmds):
        container = docker.containers.get(containerId)
        return container.exec_run(cmds,stdout=True, stderr=True, stdin=True, tty=True,
                 privileged=False, user='', detach=False, stream=True,
                 socket=False, environment=None, workdir=None)
